package me.leiusual.sharing.ui.shop

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations.switchMap
import androidx.lifecycle.ViewModel
import me.leiusual.sharing.api.model.Bank
import me.leiusual.sharing.repository.BankRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * author: leiusual
 * create on: 2019-07-29
 * description:
 */
class ShopBankEditViewModel @Inject constructor(
    private val bankRepository: BankRepository
) : ViewModel() {

  // 获取账号类型

  private val _accountType = MutableLiveData<Unit>()
  val accountType: LiveData<Data<Map<String, String>>> = switchMap(_accountType) {
    bankRepository.bankAccountType().toLiveData()
  }

  fun accountType() {
    _accountType.value = Unit
  }

  // 选中的账号类型

  private val _accountTypeSelect = MutableLiveData<Map<String,String>>()
  val accountTypeSelected: LiveData<Map<String,String>> = _accountTypeSelect
  fun accountTypeSelected(accountType: Map<String,String>) {
    _accountTypeSelect.value = accountType
  }

  // 创建或更新

  private val _bankUpdate = MutableLiveData<Bank>()
  val bankUpdate: LiveData<Data<Unit>> = switchMap(_bankUpdate) {
    bankRepository.bankUpdate(it).toLiveData()
  }

  fun bankUpdate(bank: Bank) {
    _bankUpdate.value = bank
  }
}